package com.yuxi.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Configuration    //该注解类似于spring配置文件
@Component
@PropertySource("classpath:application.properties")
public class ActivemqConfig implements EnvironmentAware {

    private Environment env;

    private Queue queue;

    private JmsTemplate j1;


    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }

    @Bean//配置一个消息队列
    public Queue queue() {
        queue = new ActiveMQQueue("sample.queue");
        return queue;
    }


    @Bean
    public ActiveMQConnectionFactory getActiveMQConnectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(env.getProperty("spring.activemq.broker-url"));
        activeMQConnectionFactory.setMaxThreadPoolSize(50);//设置最大投递数量
        return activeMQConnectionFactory;
    }

    @Bean
    @Primary
    public JmsTemplate getJmsTemplate1() {
        j1 = new JmsTemplate(getActiveMQConnectionFactory());
        j1.setReceiveTimeout(10000);
        j1.setDefaultDestination(queue());
        return j1;
    }


}
